//
//  WBMainViewController.m
//  WeiBo
//
//  Created by ChenJunWen on 2017/11/29.
//  Copyright © 2017年 CJW. All rights reserved.
//

#import "WBMainViewController.h"
#import "WBNavigationController.h"
#import "WBBaseViewController.h"
#import "WBHomeViewController.h"

@interface WBMainViewController ()

/** 撰写按钮 */
@property (nonatomic, strong) UIButton *composeBtn;
/** 字典数组 */
@property (nonatomic, strong) NSArray *dictArray;

@end

@implementation WBMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupChildViewController];
    [self setupComposeBtn];
}

#pragma mark - Private
- (void)setupChildViewController {
    self.dictArray = @[
                       @{@"className": @"WBHomeViewController", @"title": @"首页", @"imageName": @"home"},
                       @{@"className": @"WBMessageViewController", @"title": @"消息", @"imageName": @"message_center"},
                       @{@"className": @"WBBaseViewController"}, // 预留撰写按钮的位置
                       @{@"className": @"WBDiscoverViewController", @"title": @"发现", @"imageName": @"discover"},
                       @{@"className": @"WBProfileViewController", @"title": @"我", @"imageName": @"profile"},
                       ];
    
    NSMutableArray *controllersM = [NSMutableArray array];
    for (NSDictionary *dict in self.dictArray) {
        UIViewController *vc = [self controllerWithDict:dict];
        [controllersM addObject:vc];
    }
    // 设置
    self.viewControllers = controllersM;
}
- (void)setupComposeBtn {
    self.composeBtn = [UIButton jw_imageButton:@"tabbar_compose_icon_add" backgroundImageName:@"tabbar_compose_button"];
    [self.tabBar addSubview:self.composeBtn];
    
    NSInteger count = self.childViewControllers.count;
    // - 1是为了扩大撰写按钮的点击区域
    CGFloat itemWidth = self.tabBar.bounds.size.width / count - 1;
    self.composeBtn.frame = CGRectInset(self.tabBar.bounds, 2 * itemWidth, 0);
    [self.composeBtn addTarget:self action:@selector(composeBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
/** 通过字典创建控制器 */
- (UIViewController *)controllerWithDict:(NSDictionary *)dict {
    NSString *className = dict[@"className"];
    NSString *title = dict[@"title"];
    NSString *imageName = dict[@"imageName"];
    Class cls = NSClassFromString(className);
    
    UIViewController *vc = [[cls alloc] init];
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@", imageName]];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@_selected", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
    
    WBNavigationController *nav = [[WBNavigationController alloc] initWithRootViewController:vc];
    return nav;
}

- (void)composeBtnClick {
    NSLog(@"发微博");
}



@end
